#include "_ADT_Hilmans_Package.h"
#include <stdlib.h>
#include <math.h>
//-------------------------------------------------------------------
// _HilPack_General

int _HilPack_General_LongStr(char *InputS){
   int L=0;
   while ((InputS[L]!='\0')&&(L<255)){
       L++;
   }
   return L;
}

int _HilPack_General_ExistChar(char *InputS, char C){
    int L,Found=0;
    for (L=0;L<_HilPack_General_LongStr(InputS);L++){
        if (InputS[L]==C){
            Found=L+1;
            L=_HilPack_General_LongStr(InputS);           
        }
    }
    return Found;
}

int _HilPack_General_AddChr(char InputS[], char InputC){
   int L=_HilPack_General_LongStr(InputS);
   InputS[L]=InputC;
   InputS[L+1]='\0';
   return (L+1);
}

int _HilPack_General_AddChrF(char InputS[], char InputC){
   int L,Long=_HilPack_General_LongStr(InputS);
   for (L=Long;L>=1;L--)
      InputS[L]=InputS[L-1];
   InputS[0]=InputC;
   InputS[Long+1]='\0';
   return (Long+1);
}

int _HilPack_General_DelChr(char InputS[]){
   int Long=_HilPack_General_LongStr(InputS);
   if (Long<=0)
       return 0;
   else {
       InputS[Long-1]=InputS[Long];
       return 1;
   }
}

int _HilPack_General_DelChrF(char InputS[]){
   int L,Long=_HilPack_General_LongStr(InputS);
   if (Long<=0)
       return 0;
   else {
       for (L=1;L<=Long;L++){
           InputS[L-1]=InputS[L];
       } 
   }     
}

int _HilPack_General_AddStr(char S1[], char S2[]){
   int L=0;
   int buff=_HilPack_General_LongStr(S1);
   while (S2[L]!='\0'){
       S1[buff+L]=S2[L];  
       L++;
   }   
   S1[buff+L]=S2[L];
   return L;
}

int _HilPack_General_CopyStr(char Output[], char Input[]){
   int L=0;
   while (Input[L]!='\0'){
      Output[L]=Input[L];
      L++;   
   } 
   Output[L]='\0';
   return L;
}

int _HilPack_General_ParseStr(char *StrIdx, char *InputS){
   int L=0;
   int Kurung=0;
   char *Idx;
  // printf("test1\n");
   _HilPack_General_CopyStr(StrIdx,"");
   while (L<_HilPack_General_LongStr(InputS)){
       if (InputS[L]=='('){
           Kurung++;
           StrIdx[L]=(char)(Kurung);                        
       }
       else if (InputS[L]==')'){
           StrIdx[L]=(char)(Kurung);       
           Kurung--;                 
       }  
       else 
           StrIdx[L]=(char)255;
    L++;
   }
   StrIdx[L]='\0';
   return L;
}


int _HilPack_General_ParseTwo(char *One, char *Two, char *InputS){
    int L=0;
    int Kurung=0;
    if (_HilPack_General_ExistChar(InputS,',')) {
        while ((InputS[L]!=',')||(Kurung>0)&&(L<=_HilPack_General_LongStr(InputS))){
           if (InputS[L]=='('){
              Kurung++;                 
           }   
           else if (InputS[L]==')'){
              Kurung--;                 
           }   
           L++;   
        } 
        if (Kurung>0)
            return 0;
        else {
           _HilPack_General_GetStr(One,InputS,0,L-1);
           _HilPack_General_GetStr(Two,InputS,L+1,_HilPack_General_LongStr(InputS)-1);
           return _HilPack_General_ExistChar(InputS,',');
        }
    }
    else {
       return 0;   
    }
}

int _HilPack_General_ParseThree(char *One, char *Two, char* Three, char *InputS){
     char Temp[100];
     int Result;
     _HilPack_General_ParseTwo(One,Two,InputS);
     Result=_HilPack_General_ParseTwo(Temp,Three,Two);
     _HilPack_General_CopyStr(Two,Temp);
     return Result;
}

int _HilPack_General_ParseColon(char *One, char *Two, char *InputS){
    int L=0;
    int Kurung=0;
    if (_HilPack_General_ExistChar(InputS,':')){
        while ((InputS[L]!=':')||(Kurung>0)&&(L<=_HilPack_General_LongStr(InputS))){
           if (InputS[L]=='('){
              Kurung++;                 
           }   
           else if (InputS[L]==')'){
              Kurung--;                 
           }   
           L++;   
        } 
        if (Kurung>0)
            return 0;
        else {
           _HilPack_General_GetStr(One,InputS,0,L-1);
           _HilPack_General_GetStr(Two,InputS,L+1,_HilPack_General_LongStr(InputS)-1);
           return _HilPack_General_ExistChar(InputS,',');
        }
    }
    else
       return 0;
}

int _HilPack_General_IsCell ( char Input[])	{
		int found,angka=0;
		int L=0;
		while ( Input[L]==' '){
			L++;
		}
      	while ( (Input[L]>='A')&&( Input[L]<='Z') ){
			L++;
			found=1;
		}
		while ( (Input[L]>=(char)48) && (Input[L]!='\0')&& (Input[L]<=(char)57)){
			L++;
			angka=1;
		}
		if ( (Input[L] == '\0') && (found== 1)&&(angka==1))
			return 1;
		else
	        return 0;		
}

int _HilPack_General_IsNum(char InputC){
   return ((InputC>='0')&&(InputC<='9'))||(InputC=='.');
}


int _HilPack_General_ParseCell(int *OutKol, int *OutBar, char *InputS){
    int Kolom, Baris;
    int I=0,J,K;
    char S[100];
    _HilPack_General_CopyStr(S,"");
    *OutKol=0;
    *OutBar=0;
    if (_HilPack_General_IsCell(InputS)){
       while (_HilPack_General_IsChar(InputS[I])){
           *OutKol*=26;
           *OutKol+=((int)InputS[I]-64);  
           I++;
       }   
       while (_HilPack_General_IsNum(InputS[I])&&(I<_HilPack_General_LongStr(InputS))){
           *OutBar*=10;
           *OutBar+=((int)InputS[I]-48); 
           I++; 
       }
    }
}

int _HilPack_General_ParseArea(int *Kol1, int *Bar1, int *Kol2, int *Bar2, char *InputS){
   char One[100],Two[100];
   int Result;
   _HilPack_General_ParseColon(One,Two,InputS);
   _HilPack_General_ParseCell(Kol1, Bar1, One);
   Result=_HilPack_General_ParseCell(Kol2, Bar2, Two);
   return Result;
}

int _HilPack_General_AddStrF(char S1[], char S2[]){
   int L=0,Result;
   int buff=_HilPack_General_LongStr(S2);
   char S[50];
   _HilPack_General_CopyStr(S,S1);
   _HilPack_General_CopyStr(S1,S2);
   Result=_HilPack_General_AddStr(S1,S);
   
   return Result;
}

int _HilPack_General_IsArea(char *InputS){
    int L=0;
    int B=0;
    if (_HilPack_General_ExistChar(InputS,':')){
        while (L<_HilPack_General_LongStr(InputS)){
           if (InputS[L]==':'){
              B=1;
              L=_HilPack_General_LongStr(InputS);                 
           }   
           L++;
        }     
    }
    return B;
}

int _HilPack_General_GetStr(char *OutputS, char *InputS, int Start, int Stop){
   char S[50];
   _HilPack_General_CopyStr(S,"");
   int L;
   if (Start<=Stop){
     for (L=Start;L<=Stop;L++){
         _HilPack_General_AddChr(S,InputS[L]);
     }  
     _HilPack_General_CopyStr(OutputS,S);
   }
   else return 0;
}

int _HilPack_General_SameStr(char *S1, char *S2){
    int L,B=1;
    if (_HilPack_General_LongStr(S1)!=_HilPack_General_LongStr(S2))
       return 0;
    else{
        for (L=0;L<_HilPack_General_LongStr(S1);L++)
            if (S1[L]!=S2[L])
               B=0;
        return B; 
    }
}

int _HilPack_General_ExistSub(char *OutputS,char *InputS){
   int L;
   int Long=_HilPack_General_LongStr(OutputS)-_HilPack_General_LongStr(InputS);
   int B=0;
   char S[50];
   
   if (Long>=0){
       for (L=0;L<=Long;L++){
           _HilPack_General_GetStr(S,OutputS,L,L+_HilPack_General_LongStr(InputS)-1);
           if (_HilPack_General_SameStr(S,InputS)){
               B=L+1;
               L=Long+1;                                                       
           }
       }                                
   }    
   return B; 
}

int _HilPack_General_ExistSubLast(char *OutputS,char *InputS){
   int L;
   int Long=_HilPack_General_LongStr(OutputS)-_HilPack_General_LongStr(InputS);
   int B=0;
   char S[50];
   
   if (Long>=0){
       for (L=Long;L>=0;L--){
           _HilPack_General_GetStr(S,OutputS,L,L+_HilPack_General_LongStr(InputS)-1);
           if (_HilPack_General_SameStr(S,InputS)){
               B=L+1;
               L=-1;                                                       
           }
       }                                
   }    
   return B; 
}

int _HilPack_General_IsChar(char InputC){
    return ((InputC>='A')&&(InputC<='Z'));
}   
    
int _HilPack_General_IsCell_2(char *InputS){
    return (_HilPack_General_IsChar(InputS[0]));
}

int _HilPack_General_IsString (char *InputS){
    return (InputS[0]=='\'');
}

int _HilPack_General_DelStrNum(char *OutputS, int Num){
   if (Num<=_HilPack_General_LongStr(OutputS)){
       OutputS[_HilPack_General_LongStr(OutputS)-Num]='\0';                        
   }  
}

int _HilPack_General_DelStrNumF(char *OutputS, int Num){
   int L,Long=_HilPack_General_LongStr(OutputS);
   if ((Num>Long)&&(Num<=0))
       return 0;
   else {
       for (L=1;L<=Num;L++){
           _HilPack_General_DelChrF(OutputS);
       } 
   } 
}

int _HilPack_General_GetStrLastCodeStr(char *OutputS, char *InputS, char *Pattern){
   int Long=_HilPack_General_LongStr(InputS); 
   int Pos,L;
   if (!(Pos=_HilPack_General_ExistSubLast(InputS,Pattern))){
       _HilPack_General_CopyStr(OutputS,InputS);                             
   }
   else {
       _HilPack_General_GetStr(OutputS,InputS,Pos-1,Long-1); 
   }
}
